//引入express
const { request } = require('express');
const express = require('express')

//创建应用对象
const app = express()

//创建路由规则
//request是对请求报文的封装
//response是对响应报文的封装
app.get('/server',(request,response)=>{
    //设置响应头
    response.setHeader('Access-Control-Allow-Origin','*')
    //设置响应
    // response.send('HELLO AJAX')
    let data = {
        name:'atguigu'
    }
    let str = JSON.stringify(data);
    response.send(str)
})

//ie缓存问题解决
app.get('/ie',(request,response)=>{
    //设置响应头
    response.setHeader('Access-Control-Allow-Origin','*')
    //设置响应
    response.send('HELLO IE12345')
})

//延时响应
app.get('/delay',(request,response)=>{
    //设置响应头
    response.setHeader('Access-Control-Allow-Origin','*')
    //设置响应
    setTimeout(() => {
        response.send('延时响应')
    }, 3000);
})

//axios-get
app.all('/axios-server',(request,response)=>{
    //设置响应头
    response.setHeader('Access-Control-Allow-Origin','*')
    response.setHeader("Access-Control-Allow-Headers",'*');
    //设置响应
    // const data = {name:'shangguigu'};
    response.send('12345')
})

//fetch-post
app.all('/fetch-server',(request,response)=>{
    //设置响应头
    response.setHeader('Access-Control-Allow-Origin','*')
    response.setHeader("Access-Control-Allow-Headers",'*');
    //设置响应
    const data = {name:'shangguigu'};
    response.send(JSON.stringify(data))
})

//监听端口启动服务
app.listen(8000,()=>{
    console.log('服务已经启动');
})